package com.cetcs.kmga.dataManager.service.impl.sysCode;

import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.common.Page;
import com.cetcs.kmga.dataManager.dao.mybatis.SysCodeMapper;
import com.cetcs.kmga.dataManager.entity.device.DeviceInfo;
import com.cetcs.kmga.dataManager.entity.sysCode.SysCodeInfo;
import com.cetcs.kmga.dataManager.entity.sysCode.SysCodeType;
import com.cetcs.kmga.dataManager.entity.sysCode.vo.SysCodeInfoVo;
import com.cetcs.kmga.dataManager.entity.sysCode.vo.SysCodeTypeVo;
import com.cetcs.kmga.dataManager.service.sysCode.SysCodeService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.google.common.collect.Lists;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/11/15 0015.
 */
@Service
public class SysCodeServiceImpl implements SysCodeService{

    @Autowired
    private SysCodeMapper sysCodeMapper;


    @Override
    public Page getSysCodeTypeList(Data dataMap) throws Exception {
        List<SysCodeTypeVo> resultList = Lists.newArrayList();

        List<SysCodeType> list = sysCodeMapper.getSysCodeTypeList(dataMap, new PageBounds(dataMap.getInt("page"), dataMap.getInt("pageSize")));

        PageList<SysCodeType> pageList = (PageList<SysCodeType>) list;

        for (SysCodeType SysCodeType : list){
            SysCodeTypeVo vo = new SysCodeTypeVo();
            vo.setId(SysCodeType.getId());
            vo.setName(SysCodeType.getTypeName());
            resultList.add(vo);
        }

        Page page = new Page(pageList);
        page.setContent(resultList);

        return page;
    }

    @Override
    public Page getSysCodeInfoList(Data dataMap) throws Exception {

        List<SysCodeInfoVo> resultList = Lists.newArrayList();
        List<SysCodeInfo> list = sysCodeMapper.getSysCodeInfoList(dataMap, new PageBounds(dataMap.getInt("page"), dataMap.getInt("pageSize")));

        PageList<SysCodeInfo> pageList = (PageList<SysCodeInfo>) list;

        for (SysCodeInfo SysCodeInfo : list){
            SysCodeInfoVo vo = new SysCodeInfoVo();
            vo.setId(SysCodeInfo.getId());
            vo.setName(SysCodeInfo.getValue());
            vo.setCode(SysCodeInfo.getCode());
            resultList.add(vo);
        }

        Page page = new Page(pageList);
        page.setContent(resultList);

        return page;
    }

    @Override
    public int addSysCodeType(Data dataMap) throws Exception {
        return sysCodeMapper.addSysCodeType(dataMap);
    }

    @Override
    public int addSysCodeInfo(Data dataMap) throws Exception {
        return sysCodeMapper.addSysCodeInfo(dataMap);
    }

    @Override
    public int updateSysCodeType(Data dataMap) throws Exception {
        return sysCodeMapper.updateSysCodeType(dataMap);
    }

    @Override
    public int delSysCodeType(Data dataMap) throws Exception {

        int result = 0;

        if (sysCodeMapper.delSysCodeType(dataMap) > 0){
            result = sysCodeMapper.delSysCodeInfoCas(dataMap);
        }

        return result;
    }

    @Override
    public int delSysCodeInfo(Data dataMap) throws Exception {
        return sysCodeMapper.delSysCodeInfo(dataMap);
    }
}
